import Fetch from '../api/request'
const config = require('config')

/**
 * 巡检类
 */
module.exports = class InspectController {
  /**
   * 获取巡检配置
   * @param {*} ctx
   */
  static async configSearch(ctx) {
    let inspectTypeId = ctx.checkQuery('inspectTypeId').value
    let ret = await Fetch.get({
      url: '/inspect/config/search/' + inspectTypeId,
      urlPrefix: 'inspect'
    })
    ctx.body = ret
  }

  /**
   * 更新巡检配置
   * @param {*} ctx
   */
  static async configUpdate(ctx) {
    let ret
    let isError = null
    await Fetch.post({
      url: '/inspect/config/update',
      params: ctx.request.body,
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((error) => {
      isError = error
    })
    if (isError) {
      ctx.body = ctx.util.ok('', '', isError.message)
      return
    }
    ctx.body = ret
  }

  /**
   * 启动巡检任务
   * @param {*} ctx
   */
  static async doJob(ctx) {
    let environmentId = ctx.checkBody('environmentId').notEmpty().value
    let inspectObjectInfo = ctx.checkBody('inspectObjectInfo').notEmpty().value
    // 获取参数校验
    if (ctx.errors) {
      ctx.body = ctx.errors
      return
    }
    let ret
    let isError = null

    await Fetch.post({
      url: '/inspect/doJob/' + environmentId,
      params: {
        inspectObjectInfo,
        environmentId
      },
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((err) => {
      isError = err
    })

    if (isError) {
      ctx.body = ctx.util.ok('', isError.code, isError.message)
      return
    }
    ctx.body = ret
  }

  /**
   * 查询巡检项目
   * @param {*} ctx
   */
  static async itemSearch(ctx) {
    let inspectItemId = ctx.checkQuery('inspectItemId').value
    let ret
    let isError = null
    // 获取参数校验
    if (ctx.errors) {
      ctx.body = ctx.errors
      return
    }
    await Fetch.get({
      url: '/inspect/item/search/' + inspectItemId,
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((err) => {
      isError = err
    })

    if (isError) {
      ctx.body = ctx.util.ok('', isError.code, isError.message)
      return
    }
    ctx.body = ret
  }

  /**
   * 查询巡检对象
   * @param {*} ctx
   */
  static async searchObject(ctx) {
    let inspectObjectId = ctx.checkQuery('inspectObjectId').value
    let ret
    let isError = null
    // 获取参数校验
    if (ctx.errors) {
      ctx.body = ctx.errors
      return
    }
    await Fetch.get({
      url: '/inspect/object/' + inspectObjectId,
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((err) => {
      isError = err
    })

    if (isError) {
      ctx.body = ctx.util.ok('', isError.code, isError.message)
      return
    }
    ctx.body = ret
  }

  /**
   * 巡检记录查询
   * @param {*} ctx
   */
  static async record(ctx) {
    let inspectRecordId = ctx.checkQuery('inspectRecordId').value
    let ret
    let isError = null
    // 获取参数校验
    if (ctx.errors) {
      ctx.body = ctx.errors
      return
    }
    await Fetch.get({
      url: '/inspect/record/' + inspectRecordId,
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((err) => {
      isError = err
    })

    if (isError) {
      ctx.body = ctx.util.ok('', isError.code, isError.message)
      return
    }
    ctx.body = ret
  }

  /**
   * 查询各种巡检类型
   * @param ctx
   * @returns {Promise<void>}
   */
  static async inspectType(ctx) {
    let ret
    let isError = null
    await Fetch.get({
      url: '/inspect/config/findParentType',
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((error) => {
      isError = error
    })
    if (isError) {
      ctx.body = ctx.util.ok('', isError.code, isError.message)
      return
    }
    ctx.body = ret
  }

  /**
   * 获取巡检最近一次查询记录
   * @param ctx
   * @returns {Promise<void>}
   */
  static async lastRecord(ctx) {
    let ret = {}
    let isError = null
    await Fetch.get({
      url: '/inspect/record/searchLatest',
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((error) => {
      isError = error
    })
    if (isError) {
      ctx.body = ctx.util.ok('', isError.code, isError.message)
      return
    }
    ret.url = config.get('wsUrl.inspect')
    ctx.body = ret
  }

  /**
   * 获取巡检记录面板内容
   * @param ctx
   * @returns {Promise<void>}
   */
  static async recordPanel(ctx) {
    let ret
    let isError = null
    await Fetch.get({
      url: '/inspect/record/typenumber',
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((error) => {
      isError = error
    })
    if (isError) {
      ctx.body = ctx.util.ok('', isError.code, isError.message)
      return
    }
    ctx.body = ret
  }

  /**
   * 获取巡检类型日志列表
   * @param ctx
   * @returns {Promise<void>}
   */
  static async logTypeList(ctx) {
    let typeId = ctx.checkQuery('typeId').value
    let page = ctx.checkQuery('page').value
    let row = ctx.checkQuery('row').value
    let ret
    let isError = null
    // 获取参数校验
    if (ctx.errors) {
      ctx.body = ctx.errors
      return
    }
    await Fetch.get({
      url: '/inspect/record/searchByType/' + typeId,
      urlPrefix: 'inspect',
      params: {page, row}
    }).then((res) => {
      ret = res
    }).catch((error) => {
      isError = error
    })
    if (isError) {
      ctx.body = ctx.util.ok('', isError.code, isError.message)
      return
    }
    ctx.body = ret
  }

  /**
   * 获取巡检类型
   * @param {*} ctx
   */
  static async getInspectType(ctx) {
    let ret = {}
    await Fetch.get({
      url: ctx.urlHandle(),
      ctx,
      urlPrefix: 'inspect'
    }).then((res) => {
      ret = res
    }).catch((err) => {
      ctx.errorHandle(ctx, err)
    })
    ctx.body = ret
  }
}
